package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxuwz.medical.dao.FamilyDao;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.house.House;
import com.gxuwz.medical.domain.page.Page;

public class FamilyServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String m = request.getParameter("m");
		if("list".equals(m)){
			process(request,response,"/page/family/open_family.jsp");
		//	newsList(request,response);
		}else if("input".equals(m)){
			process(request,response,"/page/family/family_add.jsp");
		}else if("add".equals(m)){
			try {
				add(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("hzxx".equals(m)){   //打开户主个人信息页面
			try {
				valihz(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("tjhz".equals(m)){
			String familycode = request.getParameter("id");
			process(request,response,"/page/family/family_add1.jsp");
		}else if("del".equals(m)){
			del(request,response);
			process(request,response,"/page/family/open_family.jsp");			
		}else if("get".equals(m)){
			process(request,response,"/page/family/family_edit.jsp");
		}else if("edit".equals(m)){
			edit(request,response);
			process(request,response,"/page/family/open_family.jsp");	
		}else if("edithz".equals(m)){
			edithz(request,response);
			process(request,response,"/page/family/open_family.jsp");			
		}else if("gethz".equals(m)){
			process(request,response,"/page/family/family_edit1.jsp");
		}else if("dkhz".equals(m)){
			String familycode = request.getParameter("id");
			process(request,response,"/page/family/family_add1.jsp");
		}else if("select".equals(m)){
			select(request, response);
		}else if("newsList".equals(m)){
			newsList(request,response);
		}
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String area = request.getParameter("area");
		String housemaster = request.getParameter("housemaster");
		String sumfamily = request.getParameter("sumfamily");
		String sumfarmer = request.getParameter("sumfarmer");
		String address = request.getParameter("address");
		String createtime = request.getParameter("createtime");
		String registrar=(String)request.getSession().getAttribute("userName");
		
		String[] areaA = area.split(",");
		String areacode = areaA[0];
		String grade = areaA[1];
		
		Family family = new Family();
		
		
		String idcard = request.getParameter("idcard");
		String sex = request.getParameter("sex");	
		String birthday = request.getParameter("birthday");
		String nation = request.getParameter("nation");
		String health = request.getParameter("health");
		String culture = request.getParameter("culture");
		String relationship = request.getParameter("relationship");
		String farmer = request.getParameter("farmer");
		String marriage = request.getParameter("marriage");
		String work = request.getParameter("work");
		String phone = request.getParameter("phone");
		House house = new House();
		try{
			family.saveFamily(areacode,grade,housemaster,address,createtime,registrar,idcard,sex,birthday,nation,health,culture,relationship,farmer,marriage,work,phone);
		
			process(request,response,"/page/family/open_family.jsp");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	
	protected void edithz(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String housemaster = request.getParameter("housemaster");
		String housecode = request.getParameter("housecode");
		String idcard = request.getParameter("idcard");
		String sex = request.getParameter("sex");	
		String birthday = request.getParameter("birthday");
		String nation = request.getParameter("nation");
		String health = request.getParameter("health");
		String culture = request.getParameter("culture");
		String relationship = request.getParameter("relationship");
		String farmer = request.getParameter("farmer");
		String marriage = request.getParameter("marriage");
		String work = request.getParameter("work");
		String phone = request.getParameter("phone");
		String familycode = request.getParameter("familycode");
		House house = new House();
		
		try{
			house.editHouse(housemaster,familycode,housecode,idcard,sex,birthday,nation,health,culture,relationship,farmer,marriage,work,phone);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	protected void valihz(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String familycode = request.getParameter("familycode");
		
		House house = new House();
		boolean houseExist = house.CheckHouse(familycode);
		if (houseExist==true) {
			process(request,response,"/page/family/family_show.jsp");
		}else if(houseExist==false){
			process(request,response,"/page/family/add1_error.jsp");
		}
		
	}
	
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String familycode = request.getParameter("id");
		
		Family family = new Family();
		try{
			family.delFamily(familycode);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String familycode = request.getParameter("familycode");
		String areacode = request.getParameter("areacode");
		String sumfamily = request.getParameter("sumfamily");
		String sumfarmer = request.getParameter("sumfarmer");
		String address = request.getParameter("address");
		String createtime = request.getParameter("createtime");
//		String registrar = request.getParameter("registrar");
		String registrar=(String)request.getSession().getAttribute("userName");
		String housemaster = request.getParameter("housemaster");
		

		Family family = new Family();
		
		try{
			family.editFamily(familycode,areacode,sumfamily,sumfarmer,address,createtime,registrar,housemaster);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
	protected void select(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			// 定义一个sb来接收模糊查询的sql
			StringBuffer sb = new StringBuffer(1024);
			// form表单提交过来的用户输入的title模糊查询关键字
			String areacode = request.getParameter("areacode");
			if (areacode != null && !"".equals(areacode)) {
				sb.append(" areacode like '%" + areacode + "%'");
			}
			String familycode = request.getParameter("familycode");
			if (familycode != null && !"".equals(familycode)) {
				sb.append(" and familycode like '%" + familycode + "%'");
			}
			String housemaster = request.getParameter("housemaster");
			if (housemaster != null && !"".equals(housemaster)) {
				sb.append(" and housemaster like '%" + housemaster + "%'");
			}
			List<Family> newsList = Family.getNewsList(sb.toString());

			request.setAttribute("newsList", newsList);
			request.getRequestDispatcher("/page/family/open_family.jsp")
					.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 分页显示家庭档案信息
	 * 
	 * @param req
	 * @param resp
	 */
	private void newsList(HttpServletRequest request, HttpServletResponse response) {
		FamilyDao familyDao = new FamilyDao();
		try {
			String pageNow = request.getParameter("pageNow");
			if (pageNow == null || pageNow.isEmpty()) {
				Page<Family> newsList = familyDao.queryPage(5, 1);
				request.setAttribute("newsList", newsList);
			} else {
				Page<Family> newsList = familyDao.queryPage(5,
						Integer.parseInt(pageNow));
				request.setAttribute("newsList", newsList);
			}
			request.getRequestDispatcher("/page/family/open_family.jsp").forward(
					request, response);
		} catch (Exception e) {

		}

	}

}
